/*
 * @lc app=leetcode.cn id=872 lang=cpp
 *
 * [872] 叶子相似的树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  bool leafSimilar(TreeNode* root1, TreeNode* root2)
  {
    vector<int> v1, v2;
    DFS(root1, v1);
    DFS(root2, v2);
    if (v1.size() != v2.size())
      return false;
    else {
      for (int i = 0; i < v1.size(); ++i) {
        if (v1[i] != v2[i])
          return false;
      }
    }
    return true;
  }

  void DFS(TreeNode* root, vector<int>& seq)
  {
    if (root) {
      if (root->left == nullptr && root->right == nullptr)
        seq.push_back(root->val);
      DFS(root->left, seq);
      DFS(root->right, seq);
    }
  }
};
// @lc code=end
